﻿namespace MEF_Test.SettingsManager
{
    using System;
    using System.ComponentModel.Composition;
    using System.Configuration;

    using MEF_Test.Contracts;

    /// <summary>
    /// The default Settings Manager shipped with Mydra Center.
    /// </summary>
    /// <remarks>In order to select this <see cref="ISettingsManager"/> instance as the current Settings Manager, the Contract To Type Mapping must map the contract MEF_test.Contracts.ISettingsManager to the type MEF_Test.SettingsManager.MydraSettingsManager.</remarks>
    [Export(typeof(ISettingsManager))]
    public class MydraSettingsManager : ISettingsManager
    {
        /// <summary>
        /// Gets the setting matching the provided setting string identifier.
        /// </summary>
        /// <typeparam name="T">The type of the setting value.</typeparam>
        /// <param name="settingStringIdentifier">The setting string identifier.</param>
        /// <returns>The value for the specified setting</returns>
        public T GetSetting<T>(string settingStringIdentifier)
        {
            try
            {
                // TODO : retrieve the setting from the settings storage.
                object value = null;
                // Dirty temporary Hack 
                if (typeof(T) == typeof(int))
                {
                 value = new int() + 1337;
                    
                }

                return (T)value;
            }
            catch (InvalidCastException e)
            {
                var message =
                    String.Format(
                        "The setting value type for setting {0} does not match the specified type param {1}", 
                        settingStringIdentifier, 
                        typeof(T).FullName);

                throw new SettingsPropertyWrongTypeException(message, e);
            }
        }

        /// <summary>
        /// Sets the setting.
        /// </summary>
        /// <typeparam name="T">The type of the setting value.</typeparam>
        /// <param name="settingStringIdentifier">The setting string identifier.</param>
        /// <param name="value">The value of the setting to store.</param>
        public void SetSetting<T>(string settingStringIdentifier, T value)
        {
            throw new NotImplementedException();
        }
    }
}
